<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>

</head>
<body>
<button>发送</button>
<button>取消</button>
<script>
    let cancel = null
    const btns = document.querySelectorAll('button');
    btns[0].onclick = function (){
        if (cancel){
            // 取消上一次的请求
            cancel()
        }

        axios({
            method: 'get',
            url: 'http://127.0.0.1:3000/posts',
            cancelToken: new axios.CancelToken(function (c) {
                // 将取消请求的函数保存
                cancel = c
            })
        }).then(value => {
            cancel = null
        })
    }
    btns[1].onclick = function (){
        cancel()
    }

</script>
</body>
</html>